unit decisioncube_unt;

interface

uses
  Funcoes,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ExtCtrls, StdCtrls, Buttons, Grids, DBTables, ComCtrls,
  PivotMap_SRC, PivotGrid_SRC, PivotToolBar_SRC, PivotPaintner_SRC,
  PivotCube_SRC, TeeProcs, TeEngine, Chart, PivotChart_SRC,
  IBCustomDataSet, IBQuery, JvProgressBar, JvXPProgressBar;

type

  _pvcstring = string;
  PDoubleArray = ^DoubleArray;
  DoubleArray = array [0 .. $7FFFFFF] of double;

  Tdecisioncube_frm = class(TForm)
    PivotCube1: TPivotCube;
    PVPanel1: TPVPanel;
    PivotGrid1: TPivotGrid;
    PivotMap1: TPivotMap;
    PVDimToolBar1: TPVDimToolBar;
    PVRowToolBar1: TPVRowToolBar;
    PVColToolBar1: TPVColToolBar;
    PVMeasureToolBar1: TPVMeasureToolBar;
    JvXPProgressBar1: TJvXPProgressBar;
    IBQContador: TIBQuery;
    procedure AbreConsultaDomicilios(DataSet: TDataSet);
    procedure FechaConsultaDomicilios(DataSet: TDataSet);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure PivotCube1NextRecord(DataSet: TDataSet);
    procedure PivotMap1BeforeOpen(Sender: TObject; var Allow: Boolean);
    procedure PivotMap1CustomMeasure(Measure, Count: Integer;
      Data: OleVariant; var Value: Double);
    procedure PivotMap1DimensionSort(Sender: TObject;
      Dimension: TMapDimension; Item1, Item2: TMapDimensionItem;
      var Compare: Integer);
    procedure PVRowToolBar1Resize(Sender: TObject);
    procedure PVMeasureToolBar1ItemPaint(Sender: TObject;
      Item: TPVToolItem; Canvas: TCanvas; var Apply: Boolean);
    procedure PVMeasureToolBar1Resize(Sender: TObject);
    procedure PVColToolBar1Resize(Sender: TObject);
    procedure PVDimToolBar1ItemPaint(Sender: TObject; Item: TPVToolItem;
      Canvas: TCanvas; var Apply: Boolean);
    procedure PVDimToolBar1Resize(Sender: TObject);
  private
    { Private declarations }
    cnt: Integer;
    remd: Integer;
    stidx: Integer;
    procedure ProcessaMsg(var Msg: TMsg; var Handled: Boolean);
  public
    { Public declarations }
  end;

var
  decisioncube_frm: Tdecisioncube_frm;

implementation

{$R *.dfm}

uses DMSistema_unt, DMDados_unt, principal_unt;

procedure Tdecisioncube_frm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
   try
      DMDados_frm.IBDSDomicilio.Close;
      DMDados_frm.IBQSituacaoImovel.Close;
   except
   end;
   PivotMap1.Active := False;
   PivotCube1.Active := False;
   decisioncube_frm := nil;
   Action := caFree;
end;

procedure Tdecisioncube_frm.ProcessaMsg(var Msg: TMsg;
  var Handled: Boolean);
begin
   if Msg.message = WM_KEYDOWN then
      if not (Screen.ActiveControl is TCustomMemo) and not (Screen.ActiveControl is TButtonControl) then
      begin
         if not (Screen.ActiveControl is TCustomControl) then
         begin
            if Msg.wParam = VK_Down then Msg.wParam := VK_Tab;
            if Msg.wParam = VK_UP then
            begin
               Msg.wParam := VK_CLEAR;
               Screen.ActiveForm.Perform(WM_NextDlgCtl,1,0);
            end;
         end;
         if Msg.wParam = VK_Return then Msg.wParam := VK_Tab;
      end;
end;

procedure Tdecisioncube_frm.FormShow(Sender: TObject);
begin
   Application.OnMessage := ProcessaMsg;
   with DMDados_frm.IBQSituacaoImovel do
   begin
      try
         Close;
         Prepare;
         Open;
      except
      end;
   end;
   with DMDados_frm.IBDSDomicilio do
   begin
      try
         Close;
         AfterOpen := AbreConsultaDomicilios;
         AfterClose := FechaConsultaDomicilios;
         Prepare;
         Open;
      except
      end;
   end;
   if ((DMDados_frm.IBQSituacaoImovel.Active) and (DMDados_frm.IBDSDomicilio.Active)) then
   begin
      try
         PivotCube1.Active := True;
         PivotMap1.Active := True;
      except
      end;
   end;
end;

procedure Tdecisioncube_frm.FormCreate(Sender: TObject);
begin
   cnt := 0;
   stidx := 0;
   if PivotCube1.Active then PivotCube1.Active := False;
end;

procedure Tdecisioncube_frm.AbreConsultaDomicilios(DataSet: TDataSet);
begin
   with DMDados_frm.IBDSDomicilio do
   begin
      try
         FetchAll;
         First;
      except
      end;
   end;
   JvXPProgressBar1.Max := 100;
   with IBQContador do
   begin
      try
         Prepare;
         Open;
         remd := IBQContador.Fields[0].AsInteger div 100;
         Close;
      except
         remd := DMDados_frm.IBDSDomicilio.RecordCount div 100;
      end;
   end;
   JvXPProgressBar1.Position := 0;
end;

procedure Tdecisioncube_frm.PivotCube1NextRecord(DataSet: TDataSet);
begin
   if DataSet = DMDados_frm.IBDSDomicilio then
   begin
      if (DMDados_frm.IBDSDomicilio.Active) then
      begin
         inc(cnt);
         if (cnt mod remd) = 0 then JvXPProgressBar1.Position := JvXPProgressBar1.Position + 1;
      end;
   end;
end;

procedure Tdecisioncube_frm.FechaConsultaDomicilios(DataSet: TDataSet);
begin
   with DMDados_frm.IBDSDomicilio do
   begin
      try
         Close;
         AfterOpen := DMDados_frm.IBDSDomicilioAfterOpen;
      except
      end;
   end;
end;

procedure Tdecisioncube_frm.PivotMap1BeforeOpen(Sender: TObject;
  var Allow: Boolean);
begin
   if PivotCube1.Intf.CubeLength = 0 then
   begin
      Allow := False;
      Exit;
   end;
end;

procedure Tdecisioncube_frm.PivotMap1CustomMeasure(Measure, Count: Integer;
  Data: OleVariant; var Value: Double);
var
   i: Integer;
   pData: PDoubleArray;
begin
   Value := 0;
   pData := VarArrayLock(Data);
   try
      if Count >= 3 then
      begin
         for i := 1 to Count - 2 do
         Value := Value + pData[i];
         Dec(Count, 2);
      end
      else
      begin
         for i := 0 to Count - 1 do
         Value := Value + pData[i];
      end;
      Value := Value / Count;
   finally
      VarArrayUnLock(Data);
   end;
end;

procedure Tdecisioncube_frm.PivotMap1DimensionSort(Sender: TObject;
  Dimension: TMapDimension; Item1, Item2: TMapDimensionItem;
  var Compare: Integer);
begin
   Compare := Ord(Item2.Name[2]) - Ord(Item1.Name[2])
end;

procedure Tdecisioncube_frm.PVRowToolBar1Resize(Sender: TObject);
begin
   PVRowToolBar1.Parent.Width := PVRowToolBar1.Width;
end;

procedure Tdecisioncube_frm.PVMeasureToolBar1ItemPaint(Sender: TObject;
  Item: TPVToolItem; Canvas: TCanvas; var Apply: Boolean);
var
   i: Integer;
begin
   if PivotMap1.Active then
   begin
      for i := 0 to PivotMap1.MeasureCount - 1 do
      begin
         if TPVMeasureButton(Item).Alias = PivotMap1.Measures[i].AliasName then
         begin
            if PivotMap1.Measures[i].IsCalculated then TPVMeasureButton(Item).Settings.Color := clRed
            else
               if PivotMap1.Measures[i].mType > 3 then TPVMeasureButton(Item).Settings.Color := clOlive
               else TPVMeasureButton(Item).Settings.Color := clGreen;
            Exit;
         end;
      end;
   end;
end;

procedure Tdecisioncube_frm.PVMeasureToolBar1Resize(Sender: TObject);
begin
   PVMeasureToolBar1.Parent.Height := PVMeasureToolBar1.Height;
end;

procedure Tdecisioncube_frm.PVColToolBar1Resize(Sender: TObject);
begin
   PVColToolBar1.Parent.Height := PVColToolBar1.Height;
end;

procedure Tdecisioncube_frm.PVDimToolBar1ItemPaint(Sender: TObject;
  Item: TPVToolItem; Canvas: TCanvas; var Apply: Boolean);
var
   i: Integer;
begin
   if PivotMap1.Active then
   begin
      for i := 0 to PivotMap1.DimensionCount - 1 do
      begin
         if TPVDimButton(Item).Alias = PivotMap1.DImensions[i].AliasName then
         begin
            if i mod 3 = 0 then
            begin
               TPVDimButton(Item).Settings.Color := clYellow;
               TPVDimButton(Item).Settings.Direction := gsBottom;
               Exit;
            end
            else
               if i mod 2 = 0 then
               begin
                  TPVDimButton(Item).Settings.Color := clGreen;
                  TPVDimButton(Item).Settings.Direction := gsTop;
                  Exit;
               end
               else
               begin
                  TPVDimButton(Item).Settings.Color := clBtnFace;
                  TPVDimButton(Item).Settings.Font.Color := clWindowText;
                  TPVDimButton(Item).Settings.Style := psCtrl3D;
                  Exit;
               end;
         end;
      end;
   end;
end;

procedure Tdecisioncube_frm.PVDimToolBar1Resize(Sender: TObject);
begin
   PVDimToolBar1.Parent.Height := PVDimToolBar1.Height;
end;

end.

